/* import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

 */
import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getUserProfile, getUserDetail } from '@/api/user'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    // 存储token
    token: getToken() || '',
    // 存储用户信息
    userInfo: {}
  },
  mutations: {
    // 更新token
    setToken(state, token) {
      state.token = token
      // token存入cookie中做持久化
      setToken(token)
    },
    // 清空token
    removeToken(state) {
      state.token = ''
      // 删除cookie中的token
      removeToken()
    },
    // 更新用户信息
    setUserInfo(state, info) {
      state.userInfo = info
    },
    // 删除用户信息
    removeUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    // 登录
    async userLogin(context, forData) {
      const res = await login(forData)
      console.log('发送请求成功,获取token:', res.data)
      // 将res.data存到vuex中 调用mutation函数
      context.commit('setToken', res.data)
    },
    // 获取用户信息
    async getUserInfo(context) {
      // 获取用户基本资料
      const res = await getUserProfile()
      console.log('获取用户基本资料res是', res)
      // 获取员工基本信息
      const info = await getUserDetail(res.data.userId)
      console.log('获取员工基本信息info是', info)
      // 展开运算符 合并res info两个对象
      context.commit('setUserInfo', { ...res.data, ...info.data })
      // 返回用户页面的权限列表
      return res.data.roles.menus
    },
    // 实现退出登录
    logout(context) {
      // 删除token 和userinfo
      context.commit('removeToken')
      context.commit('removeUserInfo')
      // 退出调佣清空路由方法
      resetRouter()
    }
  }
}
